import request from "@/utils/request";

function jsonToUrlParams(obj) {
  return Object.keys(obj)
    .map((key) => {
      // 处理值为数组的情况（如：key=value1&key=value2）
      if (Array.isArray(obj[key])) {
        return obj[key]
          .map(
            (item) => `${encodeURIComponent(key)}=${encodeURIComponent(item)}`
          )
          .join("&");
      }
      // 处理普通值
      return `${encodeURIComponent(key)}=${encodeURIComponent(obj[key])}`;
    })
    .join("&");
}

const comicApi = {
  // 查询漫画列表
  list: async (params) => {
    return await request({
      url: "/app/course/selectCourse",
      method: "GET",
      params: params,
    });
  },

  // 详情
  info: async (params) => {
    return await request({
      url: "/app/course/selectCourseDetailsById",
      method: "GET",
      data: params,
    });
  },
  // 收藏
  insertCourseCollect: async (params) => {
    return await request({
      url: "/app/courseCollect/insertCourseCollect",
      method: "POST",
      data: params,
    });
  },
  // 根据id查询漫画
  read: async (params) => {
    return await request({
      url: "/app/course/selectCourseDetailsPage",
      method: "GET",
      data: params,
    });
  },
  // 查询我的阅读记录
  readHistory: async (params) => {
    return await request({
      url: "/app/courseCollect/getHistory",
      method: "GET",
      data: params,
    });
  },
  // 清空我的阅读记录
  delHistory: async (params) => {
    return await request({
      url: "/app/courseCollect/clearHistory",
      method: "GET",
      data: params,
    });
  },
  // 生成购买订单
  insertCourseOrders: async (params) => {
    return await request({
      url: "/app/order/insertCourseOrders",
      method: "GET",
      data: params,
    });
  },
  // 支付订单.实际这里指的是金币或积分支付
  payOrders: async (orderId) => {
    return await request({
      url: `/app/order/payOrders?orderId=${orderId}`,
      method: "POST",
    });
  },
  // 查询评论列表
  getCommentList: async (params) => {
    return await request({
      url: `/app/comment/page`,
      method: "GET",
      params: params,
    });
  },
  // 发布评论
  saveComment: async (params) => {
    return await request({
      url: `/app/comment/save`,
      method: "POST",
      data: params,
    });
  },
  // 检测是否超每日阅读章节数限制
  checkReadLimit: async (courseDetailsId) => {
    return await request({
      url: `/app/course/checkReadLimit?courseDetailsId=${courseDetailsId}`,
      method: "GET",
    });
  },
  // 关联推荐
  getRelateCourse: async (courseId) => {
    return await request({
      url: `/app/course/getRelateCourse?courseId=${courseId}`,
      method: "GET",
    });
  },
};

export default comicApi;
